//
//  HJInformationChildControllers.m
//  KaiChat
//
//  Created by lx on 2024/3/11.
//

#import "HJInformationChildController.h"
#import "HJShareController.h"
#import "HJFlutterController.h"
#import "HJRecommendInformationCell.h"
#import "HJInformationPresent.h"
#import "HJNewsDailyController.h"
#import "HJFlutteAlertController.h"

@interface HJInformationChildController ()

@end

@implementation HJInformationChildController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColor.tj_randomColor;
    self -> _type = [self.dict[@"type"] intValue];
    self -> _newsTypeId = [self.dict[@"news_type_id"] intValue];
        
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleFlutterPageOperateNotification:) name:flutterPageOperateNotification object:nil];
    
}

- (void)loadData {
    
}

- (void)handleFlutterPageOperateNotification:(NSNotification *)not {
    
    NSDictionary *arguments = not.object;
    NSInteger type = [arguments[@"type"] integerValue];
    if (type != self.type) {
        return;
    }
    
    NSString *informationId = arguments[@"id"];
    NSString *action = arguments[@"action"];
    if ([action isEqualToString:@"share"] || [action isEqualToString:@"forward"]) {
        [[self getDatas] enumerateObjectsUsingBlock:^(HJInformationLayout *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.model.informationId isEqualToString:informationId]) {
                obj.model.share++;
                *stop = YES;
            }
        }];
        [self reloadCell:informationId];
        return;
    }
    
    if ([action isEqualToString:@"views"]) {
        
        
        [[self getDatas] enumerateObjectsUsingBlock:^(HJInformationLayout *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.model.informationId isEqualToString:informationId]) {
                obj.model.views++;
                *stop = YES;
            }
        }];
        [self reloadCell:informationId];
        return;
    }
    
    if ([action isEqualToString:@"comment"]) {
        [[self getDatas] enumerateObjectsUsingBlock:^(HJInformationLayout *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.model.informationId isEqualToString:informationId]) {
                obj.model.comment++;
                NSLog(@"obj.model.comment = %ld", obj.model.comment);
                *stop = YES;
            }
        }];
        [self reloadCell:informationId];
        return;
    }
    
    
    if ([action isEqualToString:@"like"]) {
        [HJInformationPresent loadNewsStatisticsDataWithType:self.type newsId:informationId complete:^(NSDictionary * _Nullable data) {
            [[self getDatas] enumerateObjectsUsingBlock:^(HJInformationLayout *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([obj.model.informationId isEqualToString:informationId]) {
                    obj.model.like = [data[@"likes"] integerValue];
                    obj.model.is_like = [data[@"like_status"] intValue] == 0;
                    *stop = YES;
                }
            }];
            [self reloadCell:informationId];
        }];
        return;
    }
    
}

- (void)reloadCell:(NSString *)informationId {
    [self.tableView.visibleCells enumerateObjectsUsingBlock:^(__kindof HJRecommendInformationCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.layout.model.informationId isEqualToString:informationId]) {
            obj.layout = obj.layout;
            *stop = YES;
        }
    }];
    
}

- (NSArray *)getDatas {
    return nil;
}


- (BOOL)hiddenNavigationBar {
    return YES;
}


- (void)informationCellDidClickOperateViewButton:(HJRecommendInformationCell *)cell type:(HJInformationOperateType)type button:(UIButton *)button {
    
    HJInformationLayout *layout = cell.layout;
    
    if (type == HJInformationOperateShare) {
        HJShareController *shareController = [[HJShareController alloc] init];
        shareController.objects = @[[NSString stringWithFormat:@"%@?articleType=%@&userId=%@&articleId=%@", getNewsShareUrl(), @(self.type), HJUserInfo.userId, layout.model.informationId]];
        [shareController setSucceedBlock:^{
            [HJInformationPresent statisticsUserShareDataWithType:self.type newsId:layout.model.informationId complete:^(NSError * _Nullable error) {
                if (!error) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:flutterPageOperateNotification object:@{
                        @"type": @(self.type),
                        @"id": layout.model.informationId,
                        @"action": @"share"
                    }];
                }
            }];
        }];
        [HJTools.currentViewController presentViewController:shareController animated:YES completion:nil];
        return;
    }
    
    if (type == HJInformationOperateLike) {
        if (layout.isSendLikeRequest) {
            return;
        }
        BOOL isLike = !layout.model.is_like;
        layout.isSendLikeRequest = YES;
        [HJInformationPresent newsLikeWithType:self.type newsId:layout.model.informationId isLike:isLike complete:^(NSError * _Nullable error) {
            layout.isSendLikeRequest = NO;
            if (!error) {
                if (isLike) {
                    layout.model.like++;
                } else {
                    layout.model.like--;
                }
                layout.model.is_like = isLike;
                cell.layout = layout;
            }
        }];
        return;
    }

    if ([self isKindOfClass:HJNewsDailyController.class]) {
        return;
    }
    
    if (type == HJInformationOperateComment) {
        HJFlutteAlertController *flutterController = [[HJFlutteAlertController alloc] initWithName:HJFlutterPageNewsComment];
        flutterController.arguments = @{
            @"type": @(self.type),
            @"id": layout.model.informationId,
            @"toName": layout.model.nickname ?: @""
        };
        [self presentViewController:flutterController animated:NO completion:nil];
        return;
    }
    
    HJFlutterController *flutterController = [[HJFlutterController alloc] initWithName:HJFlutterPageNews];
    flutterController.arguments = @{
        @"type": @(self.type),
        @"id": layout.model.informationId,
    };
    [self.navigationController pushViewController:flutterController];
    
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
